package com.wocai.jrt.orgadmin.stats.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vteba.utils.common.BigDecimalUtils;
import com.wocai.jrt.orgadmin.appointment.model.Appointment;
import com.wocai.jrt.orgadmin.appointment.service.spi.AppointmentService;
import com.wocai.jrt.orgadmin.employee.model.EmployeeGroup;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeGroupService;
import com.wocai.jrt.orgadmin.investor.model.Investor;
import com.wocai.jrt.orgadmin.investor.service.spi.InvestorService;
import com.wocai.jrt.orgadmin.orders.model.Orders;
import com.wocai.jrt.orgadmin.orders.service.spi.OrdersService;
import com.wocai.jrt.orgadmin.stats.bean.GroupOdersReqBean;
import com.wocai.jrt.orgadmin.stats.service.spi.GroupPerformanceStatsService;

@Named
public class GroupPerformanceStatsServiceImpl implements GroupPerformanceStatsService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(GroupPerformanceStatsServiceImpl.class);
	
	@Inject
	private OrdersService ordersServiceImpl;
	@Inject
	private EmployeeGroupService employeeGroupServiceImpl;
	@Inject
	private InvestorService investorServiceImpl;
	@Inject
	private AppointmentService appointmentServiceImpl;
	@Override
	public Map<String, Object> groupSalesPerformance(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		DateTime endTime = startTime.plusMonths(1).minusMillis(1);
		LOGGER.info(
				"GroupStatsServiceImpl queryGroupSalesPerformance, startTime=[{}], endTime=[{}]",
				startTime, endTime);
		GroupOdersReqBean groupOdersReqBean = new GroupOdersReqBean();
		groupOdersReqBean.setGroupId(groupId);
		groupOdersReqBean.setStartTime(startTime.toDate());
		groupOdersReqBean.setEndTime(endTime.toDate());
		List<Orders> orderList = ordersServiceImpl.queryGroupSalesPerformance(groupOdersReqBean);
		// 统计订单
		List<Double> dataMonth = new ArrayList<Double>();// 每天的订单金额汇总放这儿
		int days = Days.daysBetween(startTime, endTime).getDays();
		for (int i = 0; i <= days; i++) {
			dataMonth.add(0.0);
		}
		for (Orders orders : orderList) {
			DateTime tempdDateTime = new DateTime(orders.getPayTime());
			int tempDays = Days.daysBetween(startTime, tempdDateTime).getDays();
			if (null != orders.getOrderAmount()) {
				dataMonth.set(tempDays, BigDecimalUtils.add(dataMonth.get(tempDays),orders.getOrderAmount()));
			}
		}
		// 封装数据
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取groupId对应的employeeGroup
		EmployeeGroup employeeGroup = employeeGroupServiceImpl.get(groupId);
		if (null != employeeGroup) {
			resultMap.put("name", employeeGroup.getGroupName());
		} else {
			resultMap.put("name", "");
		}
		resultMap.put("id", groupId);
		resultMap.put("data", dataMonth);
		return resultMap;
	}
	@Override
	public Map<String, Object> groupAppointmentPerformance(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		DateTime endTime = startTime.plusMonths(1).minusMillis(1);
		LOGGER.info(
				"GroupStatsServiceImpl queryGroupinvestorPerformance, startTime=[{}], endTime=[{}]",
				startTime, endTime);
		GroupOdersReqBean groupOdersReqBean = new GroupOdersReqBean();
		groupOdersReqBean.setGroupId(groupId);
		groupOdersReqBean.setStartTime(startTime.toDate());
		groupOdersReqBean.setEndTime(endTime.toDate());

		List<Appointment> appointmentList = appointmentServiceImpl
				.queryGroupAppointmentPerformance(groupOdersReqBean);
		// 统计人数
		List<Integer> dataMonth = new ArrayList<Integer>();// 每天的人数汇总放这儿
		int days = Days.daysBetween(startTime, endTime).getDays();
		for (int i = 0; i <= days; i++) {
			dataMonth.add(0);
		}
		for (Appointment appointment : appointmentList) {
			DateTime tempdDateTime = new DateTime(appointment.getCreateTime());
			int tempDays = Days.daysBetween(startTime, tempdDateTime).getDays();
			dataMonth.set(tempDays, dataMonth.get(tempDays) + 1);
		}
		// 封装数据
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取groupId对应的employeeGroup
		EmployeeGroup employeeGroup = employeeGroupServiceImpl.get(groupId);
		if (null != employeeGroup) {
			resultMap.put("name", employeeGroup.getGroupName());
		} else {
			resultMap.put("name", "");
		}
		resultMap.put("id", groupId);
		resultMap.put("data", dataMonth);
		return resultMap;
	}
	@Override
	public Map<String, Object> groupinvestorPerformance(String groupId, String date) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		DateTime startTime = DateTime.parse(date, formatter);
		DateTime endTime = startTime.plusMonths(1).minusMillis(1);
		LOGGER.info(
				"GroupStatsServiceImpl queryGroupinvestorPerformance, startTime=[{}], endTime=[{}]",
				startTime, endTime);
		GroupOdersReqBean groupOdersReqBean = new GroupOdersReqBean();
		groupOdersReqBean.setGroupId(groupId);
		groupOdersReqBean.setStartTime(startTime.toDate());
		groupOdersReqBean.setEndTime(endTime.toDate());

		List<Investor> investorList = investorServiceImpl
				.queryGroupInvestorPerformance(groupOdersReqBean);
		// 统计人数
		List<Integer> dataMonth = new ArrayList<Integer>();// 每天的人数汇总放这儿
		int days = Days.daysBetween(startTime, endTime).getDays();
		for (int i = 0; i <= days; i++) {
			dataMonth.add(0);
		}
		for (Investor investor : investorList) {
			DateTime tempdDateTime = new DateTime(investor.getRegisterTime());
			int tempDays = Days.daysBetween(startTime, tempdDateTime).getDays();
			dataMonth.set(tempDays, dataMonth.get(tempDays) + 1);
		}
		// 封装数据
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取groupId对应的employeeGroup
		EmployeeGroup employeeGroup = employeeGroupServiceImpl.get(groupId);
		if (null != employeeGroup) {
			resultMap.put("name", employeeGroup.getGroupName());
		} else {
			resultMap.put("name", "");
		}
		resultMap.put("id", groupId);
		resultMap.put("data", dataMonth);
		return resultMap;
	}
	
	
}
